const mysql = require('mysql')
const util = require('./utils')
const pool = mysql.createPool({
  host: 'localhost', //数据库地址
  user: 'lindadayo', //用户
  password: 'linpeng19980514.', //密码
  database: 'HopeTravel', //库名HopeTravel
  port: 3306
})

let query = async (sql, values ) => {
  // 返回一个 Promise
  let poolPromise = await util.to(new Promise(( resolve, reject ) => {
    pool.getConnection(function(err, connection) {
      if (err) {
        reject( err )
      } else {
        connection.query(sql, values, ( err, rows) => {
          if ( err ) {
            reject( err )
          } else {
            resolve( rows )
          }
          // 结束会话
          connection.release()
        })
      }
    })
  }))
  return poolPromise
}

module.exports =  query
